package base

import "go_gvm_mini/rtda"

type Instruction interface {
	FetchOperands(reader *ByteCodeReader) //获取操作数
	Execute(jFrame *rtda.JFrame)          //执行字节码指令
}

// NoOperandsInstruction 无操作数指令
type NoOperandsInstruction struct {
}

func (noi *NoOperandsInstruction) FetchOperands(reader *ByteCodeReader) {
	// nothing to do
}
func (noi *NoOperandsInstruction) Execute(reader *rtda.JFrame) {
	// nothing to do
}

// 跳转指令  存放偏移量
type BranchInstruction struct {
	Offset int
}

func (self *BranchInstruction) FetchOperands(reader *ByteCodeReader) {
	self.Offset = int(reader.ReadInt16())
}

func Branch(jFrame *rtda.JFrame, offset int) {
	pc := jFrame.JThread().PC()
	nextPc := pc + offset
	jFrame.SetNextPC(nextPc)
}

// iload istore
type Index8Instruction struct {
	Index uint
}

func (self *Index8Instruction) FetchOperands(reader *ByteCodeReader) {
	self.Index = uint(reader.ReadUInt8())
}

type Index16Instruction struct {
	Index uint
}

func (self *Index16Instruction) FetchOperands(reader *ByteCodeReader) {
	self.Index = uint(reader.ReadUInt16())
}
